package com.fjwt.gz.components.oss.model;

import cn.hutool.core.io.file.FileWriter;
import com.fjwt.gz.core.exception.BizException;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;

/***
* 构建一个MultipartFile 用作自定义上传
*
* @author wg
* @site https://pt.jdyd.xyz/
* @date 2022/1/18 12:33
*/
public class MockMultipartFile implements MultipartFile {

    private String originalFilename;

    private Long size;

    private InputStream inputStream;

    public MockMultipartFile(String originalFilename, Long size, InputStream inputStream){
        this.originalFilename = originalFilename;
        this.size = size;
        this.inputStream = inputStream;
    }

    @Override
    public String getName() {
        return this.originalFilename;
    }

    @Override
    public String getOriginalFilename() {
        return this.originalFilename;
    }

    @Override
    public String getContentType() {
        throw new BizException("方法未实现");
    }

    @Override
    public boolean isEmpty() {
        return false;
    }

    @Override
    public long getSize() {
        return this.size;
    }

    @Override
    public byte[] getBytes() throws IOException {
        throw new IOException("方法未实现");
    }

    @Override
    public InputStream getInputStream() throws IOException {
        return this.inputStream;
    }

    @Override
    public void transferTo(File dest) throws IOException, IllegalStateException {
        FileWriter.create(dest).writeFromStream(inputStream);
    }
}
